package formulation.controllers;

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import db.fortifier.Fortifier;
import db.nutriment.Nutriment;
import db.nutriment.NutrimentService;
import db.standard.Standard;
import db.standard.StandardService;

public class NutrimentController extends MultiActionController
{
    private NutrimentService nutrimentService;
    private StandardService standardService;

    public void setNutrimentService(NutrimentService nutrimentService) {
        this.nutrimentService = nutrimentService;
    }
    
    public void setStandardService(StandardService standardService) {
        this.standardService = standardService;
    }
    // administrator methods
    public ModelAndView admin(HttpServletRequest request,
            HttpServletResponse response)
    {
        return new ModelAndView("nutriment/admin/nutriment", "nutriments", nutrimentService.list());
    }
    
    public ModelAndView add(HttpServletRequest request,
            HttpServletResponse response, Nutriment nutriment)
    {
        nutrimentService.insert(nutriment);
        return new ModelAndView("redirect:admin.do");
    }
    
    public ModelAndView remove(HttpServletRequest request,
            HttpServletResponse response)
    {
        Long id = Long.parseLong(request.getParameter("id"));
        nutrimentService.remove(id);
        return new ModelAndView("redirect:admin.do");
    }
    // end
    public void findByIdToJson(HttpServletRequest request,
            HttpServletResponse response) throws IOException, JSONException
    {
        Long id = Long.parseLong(request.getParameter("id"));
        Nutriment nutriment = nutrimentService.find(id);
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("id", nutriment.getId());
        jsonObj.put("name", nutriment.getName());
        jsonObj.put("description", nutriment.getDescription());
        jsonObj.put("unit", nutriment.getUnit());
        response.getWriter().print(jsonObj.toString());
    }
    
    public void findFortifierByIdToJson(HttpServletRequest request,
            HttpServletResponse response) throws IOException, JSONException
    {
        Long id = Long.parseLong(request.getParameter("id"));
        Nutriment nutriment = nutrimentService.find(id);
        JSONObject jsonObj = new JSONObject();
        Iterator<Fortifier> it = nutriment.getFortifiers().iterator();
        JSONArray jsonArr = new JSONArray();
        int i = 0;
        while(it.hasNext()) {
            JSONObject jsonItem = new JSONObject();
            Fortifier fortifier = it.next();
            jsonItem.put("id", fortifier.getId());
            jsonItem.put("name", fortifier.getName());
            jsonItem.put("purity", fortifier.getPurity());
            jsonItem.put("loss", fortifier.getLoss());
            jsonItem.put("cost", fortifier.getCost());
            jsonItem.put("ratio", fortifier.getRatio());
            jsonItem.put("unit", fortifier.getUnit());
            jsonArr.put(i++, jsonItem);
        }
        jsonObj.put("fortifiers", jsonArr);
        response.getWriter().print(jsonObj.toString());
    }
    
    public void findStandardByClassificationToJson(HttpServletRequest request,
            HttpServletResponse response) throws IOException, JSONException
    {
        Long nid = Long.parseLong(request.getParameter("nid"));
        Long cid = Long.parseLong(request.getParameter("cid"));
        Standard standard = standardService.findByNutrimentIdAndClassificationId(cid, nid);
        JSONObject jsonObj = new JSONObject();
        if (standard != null) {
            jsonObj.put("id", standard.getId());
            jsonObj.put("name", standard.getName());
            jsonObj.put("description", standard.getDescription());
            jsonObj.put("min", standard.getMinInUnit());
            jsonObj.put("max", standard.getMaxInUnit());
        }
        response.getWriter().print(jsonObj.toString());
    }
    
    public ModelAndView addNutriment(HttpServletRequest request,
            HttpServletResponse response, Nutriment nutriment)
    {
        nutrimentService.insert(nutriment);
        return new ModelAndView("redirect:/frame/nutriment_form.do");
    }
    
    public ModelAndView removeNutriment(HttpServletRequest request,
            HttpServletResponse response)
    {
        Long id = Long.parseLong(request.getParameter("id"));
        nutrimentService.remove(id);
        return new ModelAndView("redirect:/frame/nutriment_form.do");
    }
}
